package com.example.lijianwen.recyclerviewtest_401ljw.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lijianwen.recyclerviewtest_401ljw.R;
import com.example.lijianwen.recyclerviewtest_401ljw.bean.TcareBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by lijianwen on 2017/4/25.
 */


public class TcareAdapter extends RecyclerView.Adapter{
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private List<TcareBean> list;
    //自定义，构造函数，传进上下文
    public TcareAdapter(Context context)
    {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //自定义：获取数据
    public void setList(List<TcareBean> list)
    {
        this.list=list;
    }
    //3自定义ViewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView tvtitle,tvdescript,tvtime;
        private Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            tvdescript=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
            button=(Button)itemView.findViewById(R.id.button);
        }
    }
    //重写：生成Item的view
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);

        return new ViewHolder(v);
    }
    //5重写：给ViewHolder中的控件填充值，设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TcareBean tcareBean = list.get(position);
        if (tcareBean == null)
            return;
        ViewHolder viewholder = (ViewHolder) holder;
        //viewholder.imageView.setImageResource(articleBean.getImgid());
        viewholder.tvtitle.setText(tcareBean.getName());
        viewholder.tvdescript.setText(tcareBean.getDescription());
        viewholder.tvtime.setText(tcareBean.getUpdate_time());

        //第三方加载图片
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"+tcareBean.getThumb()).placeholder(R.mipmap.ic_launcher).into(viewholder.imageView);
        //条目中按钮监听事件
        viewholder.button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                list.remove(position);
                notifyDataSetChanged();//更新RecycleView
                //在实际案例中，要删除应该会涉及网络或数据库访问
                //一般要有删除前的提醒
            }
        });
        //条目点击监听事件
        viewholder.itemView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                //  String id=String.valueOf(list.get(position).getArticleId());
                //  Toast.makeText(context,""+ id, Toast.LENGTH_SHORT).show();
                //实际中，点击后打开新窗口做其他操作
            }
        });
    }
    //共有多少条目
    @Override
    public int getItemCount() {
        if(list==null) return 0;
        return list.size();
    }
}
